// pages/edit_district/edit_district.js
var app = getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded',
  'cookie': "openid=" + app.globalData.openid
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    district: '',
    describe: '',
    name: '',
    telephone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let mid = options.id;

    wx.request({
      url: 'https://119.3.46.145/server/FindDepartment?id=' + mid,
      method: 'POST',
      header: header,
      success: res => {
        //数据结构·[{Description: "测试区域"， Name: "测试区域2"， admin: ""，id: "1"，phone: ""，userid: "922aeb81-29c8-41ba-9f82-36d9abb33265"}]       
        let result = JSON.parse(res.data.data)[0];
        //console.log(result, "result");
        that.setData({
          district:result.Name,
          describe: result.Description,
          name: result.admin,
          telephone: result.phone,
          mid:result.id
        });  
      },
      fail: function (error) {
        console.log(error, "FindDepartment error");
      }
    })
  },

  formSubmit: function (e) {
    let that = this;

    that.setData({
      district: e.detail.value.input_district,
      describe: e.detail.value.textarea,
      name: e.detail.value.input_name,
      telephone: e.detail.value.input_telephone
    });

    wx.request({
      url: 'https://119.3.46.145/server/UpdateDepartment?id=' + that.data.mid + '&name=' + that.data.district + '&description=' + that.data.describe + "&admin=" + that.data.name + '&phone=' + that.data.telephone,
      method: 'POST',
      header: header,
      success: res => {
        //data: { error: "0", info: "更新成功" }
        //console.log(res.data, "data");
        let result = res.data;

        wx.showModal({
          title: "提示",
          content: result.info,
          showCancel: false,
          success:function(){
            wx.navigateBack();
          }          
        });

       

        // if (result.error == "0") {
          
        // }      
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  formReset: function (e) {
    let that = this;
    that.setData({
      district: '',
      describe: '',
      name: '',
      telephone: ''
    });

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})